package com.xin.springclouduser.service.impl;

import com.xin.springclouduser.converter.User2UserDTOConverter;
import com.xin.springclouduser.converter.UserForm2UserConverter;
import com.xin.springclouduser.dataobject.User;
import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.form.UserForm;
import com.xin.springclouduser.repository.UserRepository;
import com.xin.springclouduser.service.UserService;
import com.xin.springclouduser.utils.DateUtil;
import com.xin.springclouduser.utils.KeyUtil;
import com.xin.springclouduser.vo.ResultPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Transactional
        public UserDTO create(UserForm userForm) {
        if (ObjectUtils.isEmpty(userForm)) {
            return null;
        }
        User convert = UserForm2UserConverter.convert(userForm);
        if (ObjectUtils.isEmpty(convert)) {
            return null;
        }
        convert.setId(KeyUtil.genUniqueKey());
        convert.setPassword(passwordEncoder.encode(convert.getPassword()));
        convert.setNickname(KeyUtil.genUniqueKey().substring(6));
        convert.setHeadImage("/moren.jpg");
        User user = userRepository.save(convert);
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        UserDTO userDTO = User2UserDTOConverter.convert(user);
        return userDTO;
    }

    @Transactional
    public UserDTO update(UserForm userForm) {
        if (ObjectUtils.isEmpty(userForm)) {
            return null;
        }
        User user = userRepository.findOne(userForm.getId());
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }

        user.setNickname(userForm.getNickname());
        user.setHeadImage(userForm.getHeadImage());
        user.setPhone(userForm.getPhone());
        user.setDesction(userForm.getDesction());
        User save = userRepository.save(user);
        if (ObjectUtils.isEmpty(save)) {
            return null;
        }
        UserDTO userDTO = User2UserDTOConverter.convert(save);
        return userDTO;
    }

    @Transactional
    public boolean delete(String id) {
        User user = userRepository.findOne(id);
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        userRepository.delete(user);
        return true;
    }

    @Override
    public UserDTO findOne(String id) {
        User user = userRepository.findOne(id);
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        UserDTO userDTO = User2UserDTOConverter.convert(user);
        return userDTO;
    }

    @Override
    public ResultPage<UserDTO> findAllByLikeName(String nickname, Pageable pageable) {
        Page<User> page = userRepository.findAll(nickname, pageable);
        List<User> userList = page.getContent();
        List<UserDTO> userDTOS = User2UserDTOConverter.convert(userList);
        ResultPage<UserDTO> resultPage = new ResultPage<>();
        resultPage.setTotal(page.getTotalPages());
        resultPage.setPage(page.getNumber());
        resultPage.setSize(page.getSize());
        resultPage.setData(userDTOS);
        return resultPage;
    }

    @Override
    public UserDTO findOneByPhone(String phone) {
        User user = userRepository.findByPhone(phone);
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        UserDTO userDTO = User2UserDTOConverter.convert(user);
        return userDTO;
    }

    @Override
    public UserDTO login(String phone, String password) {
        User user = userRepository.findByPhone(phone);
        if (!ObjectUtils.isEmpty(user)){
            boolean matches = passwordEncoder.matches(password, user.getPassword());
            if (matches) {
                UserDTO userDTO = User2UserDTOConverter.convert(user);
                return userDTO;
            }
            return null;
        }
        return null;
    }
}
